package com.store.service.impl;

import com.store.entity.User;
import com.store.mapper.UserMapper;
import com.store.service.UserService;
import com.store.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Map<String, Object> login(String username, String password) {
        log.info("尝试登录用户: {}", username);

        User user = userMapper.findByUsername(username);
        if (user == null) {
            log.error("用户不存在: {}", username);
            throw new RuntimeException("用户名或密码错误");
        }

        log.info("找到用户: {}, 角色: {}", username, user.getRole());

        // 直接比较密码
        if (!password.equals(user.getPassword())) {
            log.error("密码验证失败: {}", username);
            throw new RuntimeException("用户名或密码错误");
        }

        log.info("密码验证成功: {}", username);

        // 生成JWT token
        String token = jwtUtil.generateToken(username);
        log.info("生成token成功: {}", token);

        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("user", user);
        return result;
    }

    @Override
    @Transactional
    public void register(String username, String password, String role) {
        // 检查用户名是否已存在
        if (userMapper.findByUsername(username) != null) {
            throw new RuntimeException("用户名已存在");
        }

        // 创建新用户
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);  // 直接使用明文密码
        user.setRole(role);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public void registertwo(String username, String password, String role) {

    }

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User findById(Long id) {
        return userMapper.findById(id);
    }

    @Override
    @Transactional
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void update2(User user) {

    }

    @Override
    @Transactional
    public void delete(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void updateLastLoginTime(Long id) {
        userMapper.updateLastLoginTime(id, LocalDateTime.now());
    }

    @Override
    @Transactional
    public void updatePassword(Long id, String oldPassword, String newPassword) {
        User user = userMapper.findById(id);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        if (!oldPassword.equals(user.getPassword())) {
            throw new RuntimeException("原密码错误");
        }

        user.setPassword(newPassword);  // 直接使用明文密码
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    @Transactional
    public void updateRole(Long id, String role) {
        User user = userMapper.findById(id);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        user.setRole(role);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectList();
    }

    @Override
    public User getByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    @Transactional
    public User create(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userMapper.insert(user);
        return user;
    }

    @Override
    public User createtwo(User user) {
        //FANHUI
        return null;
    }

    @Override
    @Transactional
    public User update(Long id, User user) {
        user.setId(id);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
        return user;
    }

    @Override
    public long count() {
        return userMapper.count();
    }

    @Override
    public List<User> findRecentUsers(int limit) {
        return userMapper.selectRecentUsers(limit);
    }
}
